#include "http/httplib.h"
#include "HCInterface.h"
#include "json_struct.h"

#define CPPHTTPLIB_OPENSSL_SUPPORT
using namespace httplib;

int main()
{
	HCNetHardware hardware;

	// HTTP
	httplib::Server svr;

	// Capture the second segment of the request path as "id" path param
	//svr.Post("/setpos/:input", [&](const Request& req, Response& res) {
	//	std::string input = req.path_params.at("input");

	svr.Post("/setpos", [&](const Request& req, Response& res) {

		std::string jstr = req.body;

		PTZPOS pos;
		JS::ParseContext parseContext(jstr.c_str(), jstr.size(), pos);

		if (hardware.SetPTZPOS(pos))
		{
			res.set_content("sucess", "text/plain");

		}
		else
		{
			res.set_content("failed", "text/plain");

		}

		});
	svr.Post("/selzoominex", [&](const Request& req, Response& res) {

		std::string jstr = req.body;

		POINTFRAME frame;
		JS::ParseContext parseContext(jstr.c_str(), jstr.size(), frame);

		if (hardware.PTZSelZoomIn_EX(frame))
		{
			res.set_content("sucess", "text/plain");

		}
		else
		{
			res.set_content("failed", "text/plain");

		}

		});

	svr.Get("/getpos", [&](const Request& req, Response& res) {

		PTZPOS pos = hardware.GetPTZPOS();
		std::string pretty_json = JS::serializeStruct(pos, JS::SerializerOptions::Compact);
		res.set_content(pretty_json, "text/plain");
		});
	svr.Get("/getscope", [&](const Request& req, Response& res) {

		PTZSCOPE scope = hardware.GetPTZSCOPE();
		std::string pretty_json = JS::serializeStruct(scope, JS::SerializerOptions::Compact);
		res.set_content(pretty_json, "text/plain");
		});
	svr.listen("0.0.0.0", hardware.GetHttpPort());


}